(function () {
    window.Observer = {};
    let Observer = window.Observer;
    Observer.allHandles = Observer.allHandles || [];
    /**
     * 用于递归代理对象
     * @date 2020-08-21
     * @param {Object} obj 代理的目标
     * @param {Object} handle 代理的handle
     * @returns {Proxy} 返回已经代理的表层对象
     */
    Observer._Watch = function (obj, handle) {
        if (typeof obj === "object" && obj) {
            if (obj instanceof Array) {
                obj.forEach((i, index) => {
                    if (typeof i === "object" && i) {
                        obj[index] = this._Watch(i, handle);
                    }
                });
            } else {
                Object.entries(obj).forEach((i) => {
                    let [key, value] = i;
                    if (typeof value === "object" && i) {
                        obj[key] = this._Watch(value, handle);
                    }
                });
            }

            return new Proxy(obj, handle);
        } else {
            return obj;
        }
    }.bind(Observer);
    /**
     * 迭代代理的入口管理
     * @date 2020-08-21
     * @method watch
     * @param {Object} obj 需要代理的对象
     * @returns {Proxy} 已经完成迭代的对象
     * @example
     * obj = Observer.watch(obj)
     */
    window.Observer.watch = new Proxy(Observer._Watch, {
        apply: function (target, thisArg, args) {
            let handle = {
                getter: (value) => value,
                setter: (key, value) => value,
                get(target, key) {
                    if (key === "length" && target instanceof Array) {
                        return target[key];
                    }
                    let last = this.getter(target[key]);
                    return last;
                },
                set(target, key, value) {
                    if (key === "length" && target instanceof Array) {
                        target[key] = value;
                        return target;
                    }
                    let last = this.setter(key, value, target[key]);
                    if (last !== undefined) {
                        target[key] = Observer._Watch(last, this);
                    }

                    return target;
                },
            };
            let index = window.Observer.allHandles.push(handle);
            console.log("%c 这个代理的index是" + index, "color:red");
            return target(args[0], handle);
        },
    });
    console.log("%c 代理载入完成", "color:green");
})();

// let a = {
//   aa: [0, 1, 2, 3],
//   cc: {
//     name: [4, 5],
//   },
//   dd: null,
//   name: "1233",
// };
// a = Observer.watch(a);
// Observer.allHandles[0].setter = (key, value, oldValue) => {
//   console.log("%c set", "color:purple");
//   if (typeof oldValue === "number") {
//     if (oldValue < value) {
//       console.log(value, "插入成功");
//       return value;
//     } else {
//       console.log(key, value);
//       console.log("值错误,未插入");
//       return;
//     }
//   }
//   return value;
// };
// Observer.allHandles[0].getter = (value) => {
//   console.log("%c get", "color:res");
//   if (typeof value === "number") {
//     return value + 11;
//   }
//   return value;
// };

// a.aa.push({
//   name: 11,
// });
